package com.sixth.controller;

import com.fasterxml.jackson.databind.ser.Serializers;
import com.sixth.bean.MarketKeyword;
import com.sixth.bean.bo.KeywordCreateBO;
import com.sixth.bean.bo.KeywordListBO;
import com.sixth.bean.bo.KeywordBO;
import com.sixth.bean.common.BaseRespVo;
import com.sixth.bean.vo.KeywordCreateVO;
import com.sixth.bean.vo.KeywordListVO;
import com.sixth.bean.vo.KeywordUpdateVO;
import com.sixth.constant.Constant;
import com.sixth.service.AdminKeywordService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ljh
 * @version 1.0
 * @describition:
 * @since 2022/9/7 14:42
 **/

@RestController
@RequestMapping("admin/keyword")
public class AdminKeywordController {

    @Autowired
    AdminKeywordService adminKeywordService;

    //list?page=1&limit=20&sort=add_time&order=desc
    @RequiresPermissions(value = {"admin:keyword:list"},logical = Logical.OR)
    @RequestMapping("list")
    public BaseRespVo list(KeywordListBO keywordListBO){
        KeywordListVO<MarketKeyword> keywordList = adminKeywordService.list(keywordListBO);
        if(keywordList.getList() != null){
            return BaseRespVo.ok(keywordList);
        }
        return BaseRespVo.invalidData();
    }

    //{keyword: "1111", url: "111", isHot: true, isDefault: true}
    //{"errno":0,"data":{"id":16,"keyword":"123456","url":"123",
    // "isHot":true,"isDefault":true,"addTime":"2022-09-07 16:03:51",
    // "updateTime":"2022-09-07 16:03:51"},"errmsg":"成功"}
    @RequestMapping("create")
    @RequiresPermissions(value = {"admin:keyword:create"},logical = Logical.OR)
    public BaseRespVo create(@RequestBody KeywordCreateBO keywordCreateBO){
        MarketKeyword marketKeyword = adminKeywordService.create(keywordCreateBO);
        if (marketKeyword == null) {
            return BaseRespVo.invalidData();
        }

        KeywordCreateVO keywordVO = new KeywordCreateVO();
        keywordVO.setId(marketKeyword.getId());
        keywordVO.setKeyword(marketKeyword.getKeyword());
        keywordVO.setUrl(marketKeyword.getUrl());
        keywordVO.setIsHot(marketKeyword.getIsHot());
        keywordVO.setIsDefault(marketKeyword.getIsDefault());
        keywordVO.setAddTime(marketKeyword.getAddTime());
        keywordVO.setUpdateTime(marketKeyword.getUpdateTime());

        return BaseRespVo.ok(keywordVO);
    }

    //update
    //addTime: "2022-09-07T08:34:06.173+00:00"
    // id: 10. isDefault: false. isHot: false. keyword: "dsada".
    // updateTime: "2022-09-07T08:34:06.173+00:00".url: "dsada"
    //{"id":21,"keyword":"hjgfhf","url":"hfgh","isHot":false,"isDefault":true,"addTime":"2022-09-07 16:34:49","updateTime":"2022-09-07 16:39:56"}
    @RequiresPermissions(value = {"admin:keyword:update"},logical = Logical.OR)
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody KeywordBO keywordBO){

        MarketKeyword marketKeyword = adminKeywordService.update(keywordBO);

        if(marketKeyword == null){
            return BaseRespVo.invalidData();
        }

        KeywordUpdateVO keywordVO = new KeywordUpdateVO();
        keywordVO.setId(marketKeyword.getId());
        keywordVO.setKeyword(marketKeyword.getKeyword());
        keywordVO.setUrl(marketKeyword.getUrl());
        keywordVO.setIsHot(marketKeyword.getIsHot());
        keywordVO.setIsDefault(marketKeyword.getIsDefault());
        keywordVO.setAddTime(marketKeyword.getAddTime());
        keywordVO.setUpdateTime(marketKeyword.getUpdateTime());

        return BaseRespVo.ok(keywordVO);
    }

    //http://localhost:8083/admin/keyword/delete
    @RequiresPermissions(value = {"admin:keyword:delete"},logical = Logical.OR)
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody KeywordBO keywordBO){

        int code = adminKeywordService.delete(keywordBO);

        if(code == Constant.NON){
            return BaseRespVo.invalidData();
        }

        return BaseRespVo.ok();
    }

}
